<?php

namespace App\Http\Middleware;

use App\Services\Log;
use Exception;
use App\Exceptions\ApiException;
use  App\Models\UserTicket;
use Closure;
use Illuminate\Support\Facades\Input;

class ApiTicketVerify
{
    /**
     * Handle an incoming request
     * @param $request
     * @param Closure $next
     * @return mixed
     * @throws ApiException
     */
    public function handle($request, Closure $next)
    {
        try{
            $ticket = $request->input('ticket');
            $client_source = $request->input('ClientSource');

            $result = checkTicket($ticket);
            if (!$result) {
                $_SERVER['message_code'] = 10137;
                throw new ApiException('验证ticket失败',0);
            }
        } catch (Exception $e) {
            if ($e instanceof ApiException)
            {
                throw $e;
            }
            else
            {
                throw new ApiException($e->getMessage(),$e->getCode(),$e);  
            }
        }
        return $next($request);
        
    }
}
